package sms_interface;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Random;
import org.json.JSONObject;
/**
 * 
 * @author aayushjain
 * Coordinator class is used for simulation of our application.
 * It takes the http request query and returns the message to be sent to the user.
 *
 */
public class Coordinator {
	/**
	 * Coordinate function is used for simulation of our application.
	 * It takes the http request query and returns the message to be sent to the user.
	 * @param rest_query Http request query formed from the message
	 * @return Message in response to the query that was passed as the argument.
	 * @throws Exception The mysql table table asked for may not be there.
	 */
	
public static String coordinate(String rest_query) throws Exception
{
	String Url = "jdbc:mysql://localhost:3306/data";
	String query = "Select * FROM data_table Limit ";
	Random ran=new Random();
	int num=ran.nextInt(5000);
	System.out.println(num);
	query=query+num+",1000";
	Class.forName("com.mysql.jdbc.Driver");
	Connection con = DriverManager.getConnection (Url,"root","aayush");
	Statement stmt = con.createStatement();
	ResultSet rs = stmt.executeQuery(query);
	String [] arr2=rest_query.split("/");
	//System.out.println(rs);
	
	JSONObject[] que=new JSONObject[5];
	que[0]=new JSONObject().put("location", "");
	que[1]=new JSONObject().put("appliance", "");
	que[2]=new JSONObject().put("aid", "");
	que[3]=new JSONObject().put("state", "");
	que[4]=new JSONObject().put("ERRCODE", "");
	
	String abcd="[";
	for( int i=0;i<5;i++)
	{
		abcd=abcd+que[i].toString()+",";
	}
	abcd=abcd+"]";
	//System.out.println(abcd);
	String fin_object="{\"actuate\":"+ abcd+"}";
	//System.out.println(fin_object);
	
	
	
	rs.next();
	String message="Sms received but utility not available yet.";
	if(arr2[2].equalsIgnoreCase("get"))
	{
		//System.out.println(arr2[1]);		
		
		if(arr2[1].equalsIgnoreCase("state"))
		{
			//System.out.println(arr2[1]);		
			String state;
			int status=rs.getInt(2);
			if( status==1){
				state="ON";
			}
			else state="OFF";
			message="Status of "+arr2[3]+" of "+arr2[4]+":"+state+"("+status+")";
			//System.out.println(status);
		}
		else if(arr2[1].equalsIgnoreCase("data"))
		{
			int entrycount=0;
			int [] Doorarr=new int[10];
			double [] Temparr=new double[10];
			while(entrycount!=10)
			{
			Doorarr[entrycount]=rs.getInt(3);
			Temparr[entrycount]=rs.getDouble(5);
			entrycount++;
			int count=0;
			while(count!=10)
				{
					rs.next();
					count++;
				}
			
			}
			message="Data(10 sec gap/entry):Door Sensor";
			for(int i=0;i<10;i++){
				message=message+Doorarr[i]+",";
			}
			message=message+" Temp sensor";
			for(int i=0;i<10;i++){
				message=message+Temparr[i]+",";
			}
		}
	}
	return message;
}
 




/**
 * For checking the coordinate module.
 * @param args
 * @throws Exception
 */
public static void main(String args[]) throws Exception
{
	System.out.println(coordinate("100/state/get/f1/l1"));	
	System.out.println(coordinate("100/data/get/f1/l1/2011-10-25:15-51-45/2011-10-25:15-52-33"));
}

}